package com.duowei.diandan.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.MealChildChecked;
import com.duowei.diandan.databinding.MealHItemBinding;
import com.duowei.diandan.db.entry.Tcsd;
import com.duowei.diandan.util.FoodHelper;

import java.util.Collections;
import java.util.List;

public class MealHAdapter extends RecyclerView.Adapter<MealHAdapter.ViewHolder> {

    private List<Tcsd> mList;
    private int mCheckedPos = -1;

    private MealChildChecked mCheckedItem;

    public MealHAdapter(List<Tcsd> list, MealChildChecked checkedItem) {
        mList = list;
        mCheckedItem = checkedItem;

        if (list.size() == 1) {
            mCheckedPos = 0;
        } else {
            for (int i = 0; i < mList.size(); i++) {
                Tcsd t = mList.get(i);
                if ("1".equals(t.getSfxz())) {
//                Collections.swap(mList, i, 0);
                    Collections.rotate(mList, mList.size() - i);
                    mCheckedPos = 0;

                    //
                    mCheckedItem.setTcsd(mList.get(0));
                    break;
                }
            }
        }
    }

    @Override
    public MealHAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        MealHItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.meal_h_item, parent, false);

//        int w = (context.getResources().getDisplayMetrics().widthPixels - 25) / Settings.COLUMN_COMBO;
//        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(w, w);
//        binding.frameLayout.setLayoutParams(params);

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(MealHAdapter.ViewHolder holder, int position) {
        MealHItemBinding binding = holder.mBinding;
        Tcsd item = mList.get(holder.getAdapterPosition());

        FoodHelper.setCoverImage(item.getImgNo(), binding.coverView);
        binding.nameView.setText(item.getName());

        Float dj = item.getDj();
        if (dj != null && dj > 0) {
            binding.priceDescView.setVisibility(View.VISIBLE);
            binding.priceDescView.setPrice(item.getDj());
        } else {
            binding.priceDescView.setVisibility(View.GONE);
            binding.priceDescView.setText(null);
        }

        if (mCheckedPos != -1 && mCheckedPos == holder.getAdapterPosition()) {
//            binding.frameLayout.setChecked(true);
            binding.frameLayout.setVisibility(View.VISIBLE);
            binding.checkedView.setVisibility(View.VISIBLE);
        } else {
//            binding.frameLayout.setChecked(false);
            binding.frameLayout.setVisibility(View.GONE);
            binding.checkedView.setVisibility(View.GONE);
        }

        binding.getRoot().setOnClickListener(v -> {
            if (mCheckedPos != -1 && mCheckedPos != holder.getAdapterPosition()) {
                int lastPos = mCheckedPos;
                mCheckedPos = holder.getAdapterPosition();
                notifyItemChanged(lastPos);
                notifyItemChanged(mCheckedPos);

                //
                mCheckedItem.setTcsd(item);
            } else {
                mCheckedPos = holder.getAdapterPosition();
                notifyItemChanged(mCheckedPos);
            }

            for (int i = 0; i < mList.size(); i++) {
                Tcsd tcsd = mList.get(i);
                if (i == mCheckedPos) {
                    tcsd.setSfxz("1");
                } else {
                    tcsd.setSfxz(null);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        MealHItemBinding mBinding;

        public ViewHolder(MealHItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }
}
